﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using GeekShop.Properties;

namespace GeekShop.Commands.Fill
{
    public class Gradient : ICommand
    {
        #region Fields
        private Picture picture;
        private Bitmap oldBitmap;
        private Point begin;
        private Point end;
        private GeekShop.Gradient gradient;
        #endregion

        #region Contrustor

        /// <summary>Creates a Command which fills a layer with a color gradient.</summary>
        /// <param name="layer">the target layer</param>
        /// <param name="begin">the gradient's starting point</param>
        /// <param name="end">the gradient's ending point</param>
        /// <param name="gradient">the color gradient to fill the specified layer with</param>
        public Gradient(Picture picture, Point begin, Point end, GeekShop.Gradient gradient)
        {
            this.picture = picture;
            this.begin = begin;
            this.end = end;
            this.gradient = gradient;
        }
        #endregion

        #region Private Methods
        private System.Drawing.Color CalculateColor(int x, int y)
        {
            return this.CalculateColor(new Point(x, y));
        }
        private System.Drawing.Color CalculateColor(Point location)
        {
            //todo: color calculation for gradient fill
            return System.Drawing.Color.Black;
        }
        #endregion

        #region ICommand Members
        public void Do()
        {
            Layer selectedLayer = this.picture.SelectedLayer;
            this.oldBitmap = (Bitmap)selectedLayer.Bitmap.Clone();
            for (int x = 0; x < selectedLayer.Bitmap.Size.Width; x++)
            {
                for (int y = 0; y < selectedLayer.Bitmap.Size.Height; y++)
                {
                    if (this.picture.Mask == null || this.picture.Mask.Bumpmap.GetPixel(x, y).GetBrightness() > 120)
                    {
                        selectedLayer.Bitmap.SetPixel(x, y, this.CalculateColor(x, y));
                    }
                }
            }    
        }
        public void Undo()
        {
            this.picture.SelectedLayer.Bitmap = (Bitmap)this.oldBitmap.Clone();
        }
        #endregion
    }
}
